﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Lcygcc.Infrastructure.Logs;

namespace Lcygcc.Infrastructure.Repository
{
    public class BaseRepository : IBaseRepository, IDisposable
    {
        private static string m_IsPrintSQL;
        private SqlSugarClient m_Database;
        public static string IsPrintSQL
        {
            get
            {
                BaseRepository.m_IsPrintSQL = "0";
                return BaseRepository.m_IsPrintSQL;
            }
        }
        public SqlSugarClient db
        {
            get
            {
                //SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
                //{
                //    ConnectionString = DbConfig.MainConnectionString,
                //    DbType = "MySql",
                //    IsAutoCloseConnection = true
                //});
                SqlSugarClient db = DbFactory.GetSqlSugarClient();
                db.Ado.IsEnableLogEvent = (BaseRepository.IsPrintSQL == "1");
                db.MappingTables.AddRange(DbMappingConfig.MpTableList);
                db.Ado.LogEventStarting = delegate (string sql, string pars)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine("\r\n==============start sql======================");
                    stringBuilder.AppendLine("\r\n" + sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
                    stringBuilder.AppendLine("\r\n==============end sql  ======================");
                };
                return db;
            }
        }
        public SqlSugarClient Instance()
        {
            return this.GetInstance(ref this.m_Database, true, "vpclub-cloud");
        }
        private SqlSugarClient GetInstance(ref SqlSugarClient sugarClient, bool useWriteConnection = true, string connectionStringName = "vpclub-cloud")
        {
            SqlSugarClient result;
            try
            {
                //SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
                //{
                //    ConnectionString = DbConfig.MainConnectionString,
                //    DbType = "MySql",
                //    IsAutoCloseConnection = true
                //});
                SqlSugarClient db = DbFactory.GetSqlSugarClient();
                db.Ado.IsEnableLogEvent = false;
                db.MappingTables.AddRange(DbMappingConfig.MpTableList);
                db.Ado.LogEventStarting = delegate (string sql, string pars)
                {
                    Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
                    Console.WriteLine();
                };
                result = db;
            }
            catch (Exception ex)
            {
                Logger.Error("-----BaseRepository.GetInstance-----", ex);
                result = null;
            }
            return result;
        }
        public void Dispose()
        {
            if (this.m_Database != null)
            {
                this.m_Database.Dispose();
            }
        }
    }
}
